#include "head.h"

// 单链表的创建
U_list *S_List_init()
{
    // 创建头节点
    U_list *list = (U_list *)malloc(sizeof(U_list));
    // 判断是否创建成功
    if (list != NULL)
    {
        // 如果成功则初始化链表中的头节点为 NULL
        list->next = NULL;
    }
    else
    {
        printf("创建失败\n");
        return NULL;
    }
    // 返回链表结构体
    return list;
}

// 单链表的插入
void S_List_insert(U_list *list, U_data data, int pos)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }

    U_node *new_node = (U_node *)malloc(sizeof(U_node));
    if (new_node == NULL)
    {
        printf("new_node创建失败\n");
        return;
    }
    new_node->next = NULL;
    new_node->data = data;
    if (pos == 1)
    {
        new_node->next = list->next;
        list->next = new_node;
        return;
    }

    U_node *ptr = list->next;

    for (size_t i = 1; i < pos - 1 && ptr != NULL; i++)
    {
        ptr = ptr->next;
    }

    if (ptr == NULL)
    {
        printf("ptr已经指向NULL\n");
        free(new_node);
        return;
    }
    new_node->next = ptr->next;
    ptr->next = new_node;

    return;
}

// 单链表的删除
void S_List_delete(U_list *list, int pos)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }

    U_node *ptr_1 = list->next;
    U_node *ptr_2 = NULL;
    if (pos == 1)
    {
        list->next = ptr_1->next;
        free(ptr_1);
        return;
    }

    for (size_t i = 0; i < pos - 1 && ptr_1->next != NULL; i++)
    {
        ptr_2 = ptr_1;
        ptr_1 = ptr_1->next;
    }
    if (ptr_1 == NULL)
    {
        printf("ptr_1指向null\n");
        return;
    }
    ptr_2->next = ptr_1->next;

    free(ptr_1);
    return;
}

// 按名字删除单链表节点
void S_List_delete_name(U_list *list, char *name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }

    U_node *ptr_1 = list->next;
    U_node *ptr_2 = NULL;
    int found = 0;
    while (ptr_1 != NULL)
    {
        if (strcmp(ptr_1->data.name, name) == 0)
        {
            found = 1;
            break;
        }
        ptr_2 = ptr_1;
        ptr_1 = ptr_1->next;
    }
    if (!found)
    {
        printf("未找到名为%s的节点\n", name);
        return;
    }
    if (ptr_2 == NULL) // 如果删除的是头节点，需要更新头节点指针
    {
        list->next = ptr_1->next;
    }
    else // 如果删除的不是头节点，需要更新前一个节点的指针
    {
        ptr_2->next = ptr_1->next;
    }
    free(ptr_1); // 释放内存空间
    return;
}


// 单链表的查找
U_node *S_List_find_name(U_list *list, char *name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return NULL;
    }

    U_node *ptr = list->next;
    while (ptr != NULL)
    {
        if (strcmp(ptr->data.name, name) == 0)
        {
            return ptr;
        }
        ptr = ptr->next;
    }

    return ptr;
}

// 单链表的按行号查找
U_node *S_List_find_line(U_list *list, int line)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return NULL;
    }

    U_node *ptr = list->next;
    int current_line = 1;
    while (ptr != NULL)
    {
        if (current_line == line)
        {
            return ptr;
        }
        ptr = ptr->next;
        current_line++;
    }

    return NULL;
}

// 单链表的遍历
void S_List_print(U_list *list)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }
    U_node *ptr = list->next;
    int i = 0;
    while (ptr != NULL)
    {
        printf("%d", ++i);
        printf("用户名:%s\t密码:%s\t身份码:%d\t余额:%d\n",
               ptr->data.name, ptr->data.passwd, ptr->data.grade, ptr->data.value);
        ptr = ptr->next;
    }
}

// 单链表的清空
void S_List_clear(U_list *list)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }

    U_node *ptr_1 = list->next;
    U_node *ptr_2 = ptr_1->next;

    while (ptr_1 != NULL)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if (ptr_2 != NULL)
        {
            ptr_2 = ptr_2->next;
        }
    }
    list->next = NULL;
}

// 单链表的合并
U_list *S_List_merge(U_list *list1, U_list *list2)
{
    if (list1 == NULL || list2 == NULL)
    {
        printf("链表不存在\n");
        return list1;
    }

    // 1、求链表list1 的链表长度
    U_node *ptr_1 = list1->next;
    int len = 0;

    while (ptr_1 != NULL)
    {
        ptr_1 = ptr_1->next;
        len++;
    }
    // 插入到链表中
    ptr_1 = list1->next;
    U_node *ptr_2 = list2->next;
    while (ptr_2 != NULL)
    {
        len++;
        S_List_insert(list1, ptr_2->data, len);
        ptr_2 = ptr_2->next;
    }

    return list1;
}

// 单链表的销毁
void S_List_free(U_list **list)
{
    if (list == NULL || *list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }

    U_node *ptr_1 = (*list)->next;
    U_node *ptr_2 = ptr_1->next;

    while (ptr_1 != NULL)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if (ptr_2 != NULL)
        {
            ptr_2 = ptr_2->next;
        }
    }

    free(*list);
    *list = NULL;
}

// 向文件写入用户信息
void S_List_File_print(U_list *list, const char *file_name)
{
    // 安全判断
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    // if (list->next == NULL)
    // {
    //     printf("链表无数据\n");
    //     return;
    // }

    FILE *fp = fopen(file_name, "w+");
    if (fp == NULL)
    {
        printf("文件创建失败\n");
        return;
    }
    fseek(fp, 0, SEEK_SET);

    U_node *ptr = list->next;

    while (ptr != NULL)
    {
        fprintf(fp, "用户名:%s\t密码:%s\t身份码:%d\t余额:%d\n",
                ptr->data.name, ptr->data.passwd, ptr->data.grade, ptr->data.value);
        ptr = ptr->next;
    }

    fflush(fp); // 刷新文件

    fclose(fp);
}

// 从文件读取用户信息
void S_List_File_scanf(U_list *list, const char *file_name)
{
    // 安全判断
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }

    FILE *fp = fopen(file_name, "r");
    if (fp == NULL)
    {
        printf("文件打开失败\n");
        return;
    }
    // 移动光标
    fseek(fp, 0, SEEK_SET);
    // 清空链表
    S_List_clear(list);

    U_data data;
    char ch = 0;
    int pos = 0;

    // 检测文件行数
    while ((ch = fgetc(fp)) != EOF)
    {
        if (ch == '\n')
        {
            pos++;
        }
    }

    // 移动光标
    fseek(fp, 0, SEEK_SET);

    for (size_t i = 0; i < pos; i++)
    {
        // "用户名:%s\t密码:%s\t身份码:%d\t余额:%d\n"
        fscanf(fp, "用户名:%s\t密码:%s\t身份码:%d\t余额:%d\n",
               data.name, data.passwd, &data.grade, &data.value);
        S_List_insert(list, data, i + 1);
    }

    fflush(fp); // 刷新文件

    fclose(fp);
}

// 修改用户等级
void S_List_update_grade(U_list *user_list, int pos, FlightSystem Flight_sys, int flag)
{
    U_node *ptr_1 = NULL;
    int new_grade;
    printf("请输入你要改的等级值：\n");
    scanf("%d", &new_grade);
    if (pos == 1)
    {
        // user_list->next->data.grade = new_grade;
        ptr_1 = user_list->next;
    }

    for (size_t i = 0; i < pos - 1 && user_list->next != NULL; i++)
    {
        ptr_1 = user_list;
        user_list = user_list->next;
    }
    if (user_list == NULL)
    {
        printf("user_list指向null\n");
    }
    user_list->next->data.grade = new_grade;

    switch (new_grade)
    {
    case 1:
        S_List_insert(Flight_sys.ADMIN_USER, ptr_1->data, 1);
        // S_List_print(Flight_sys.ADMIN_USER);
        S_List_File_print(Flight_sys.ADMIN_USER, ADMIN_FILE);

        break;
    case 2:
        S_List_insert(Flight_sys.REGULAR_USER, ptr_1->data, 1);
        // S_List_print(Flight_sys.REGULAR_USER);
        S_List_File_print(Flight_sys.REGULAR_USER, REGULAR_FILE); //写入普通用户文件

        break;
    case 3:
        S_List_insert(Flight_sys.SILVER_USER, ptr_1->data, 1);
        // S_List_print(Flight_sys.SILVER_USER);
        S_List_File_print(Flight_sys.SILVER_USER, SILVER_FILE); //写入银卡用户文件

        break;
    case 4:
        S_List_insert(Flight_sys.GOLD_USER, ptr_1->data, 1);
        // S_List_print(Flight_sys.GOLD_USER);
        S_List_File_print(Flight_sys.GOLD_USER, GOLD_FILE); //写入金卡用户文件

        break;
    case 5:
        S_List_insert(Flight_sys.PLATINUM_USER, ptr_1->data, 1);
        // S_List_print(Flight_sys.PLATINUM_USER);
        S_List_File_print(Flight_sys.PLATINUM_USER, PLATINUM_FILE);

        break;

    default:
        break;
    }

    if (flag == 1)
    {
        S_List_delete(Flight_sys.PLATINUM_USER, pos);
        S_List_File_print(Flight_sys.PLATINUM_USER, PLATINUM_FILE);
    }
    else if (flag == 2)
    {
        S_List_delete(Flight_sys.GOLD_USER, pos);
        S_List_File_print(Flight_sys.GOLD_USER, GOLD_FILE); //写入金卡用户文件
    }
    else if (flag == 3)
    {
        S_List_delete(Flight_sys.SILVER_USER, pos);
        S_List_File_print(Flight_sys.SILVER_USER, SILVER_FILE); //写入银卡用户文件
    }
    else if (flag == 4)
    {
        S_List_delete(Flight_sys.REGULAR_USER, pos);
        S_List_File_print(Flight_sys.REGULAR_USER, REGULAR_FILE); //写入普通用户文件
    }

    return;
}

// 修改用户信息
void S_List_update_info(U_list *user_list, int user_id, char *new_name, char *new_password, int flag, FlightSystem Flight_sys)
{
    U_node *ptr_1 = (U_node *)malloc(sizeof(U_node));
    if (ptr_1 == NULL)
    {
        // 处理内存分配失败的情况
        return;
    }
    else
    {
        ptr_1 = S_List_find_line(user_list, user_id);
        // 创建一个临时变量来存储新的名称
        char temp_name[60];
        char temp_passwd[70];
        strcpy(temp_name, new_name);
        //temp_name[59] = '\0';
        strcpy(temp_passwd, new_password);
        //temp_passwd[69] = '\0';

        // 将临时变量的值赋给 ptr_1->data.name
        strcpy(ptr_1->data.name, "0");
        strcpy(ptr_1->data.name, temp_name);
        //ptr_1->data.name[59] = '\0';

        strcpy(ptr_1->data.passwd, "0");
        strcpy(ptr_1->data.passwd, temp_passwd);
        //ptr_1->data.name[59] = '\0';
    }

    // S_List_print(user_list);

    S_List_insert(user_list, ptr_1->data, user_id);
    S_List_delete(user_list, user_id + 1);
    printf("修改成功\n");
    if (flag == 1)
    {
        S_List_File_print(Flight_sys.PLATINUM_USER, PLATINUM_FILE);
    }
    else if (flag == 2)
    {
        S_List_File_print(Flight_sys.GOLD_USER, GOLD_FILE); //写入金卡用户文件
    }
    else if (flag == 3)
    {
        S_List_File_print(Flight_sys.SILVER_USER, SILVER_FILE); //写入银卡用户文件
    }
    else if (flag == 4)
    {
        S_List_File_print(Flight_sys.REGULAR_USER, REGULAR_FILE); //写入普通用户文件
    }
    return;
}

//用户修改密码
void user_change_password(U_data *log_data, FlightSystem Flight_sys, char *new_passwd)
{
    if (log_data == NULL || new_passwd == NULL)
    {
        printf("Invalid input\n");
        return;
    }

    // Create a copy of log_data to avoid modifying the original data
    U_data new_user;
    memcpy(&new_user, log_data, sizeof(U_data)); //复制

    // Copy the new password to the copied user data
    strncpy(new_user.passwd, new_passwd, sizeof(new_user.passwd) - 1);
    new_user.passwd[sizeof(new_user.passwd) - 1] = '\0'; // Ensure null termination

    printf("修改成功\n");
    // if (new_user.grade == 2)
    // {
    //     printf("aaaaaaaaaaa");
    //     S_List_delete_1(Flight_sys.GOLD_USER, *log_data); // 传入指针的地址以修改原始指针
    //     S_List_insert(Flight_sys.GOLD_USER, new_user, 1);
    //     S_List_File_print(Flight_sys.GOLD_USER, GOLD_FILE);
    // }
    // else if (new_user.grade == 3)
    // {
    //     printf("aaaaaaaaaaa");
    //     S_List_delete_1(Flight_sys.SILVER_USER, *log_data); // 传入指针的地址以修改原始指针
    //     S_List_insert(Flight_sys.SILVER_USER, new_user, 1);
    //     S_List_File_print(Flight_sys.SILVER_USER, SILVER_FILE);
    // }
    // else if (new_user.grade == 4)
    // {
    //     printf("aaaaaaaaaaa");
    //     S_List_delete_1(Flight_sys.REGULAR_USER, *log_data); // 传入指针的地址以修改原始指针
    //     S_List_insert(Flight_sys.REGULAR_USER, new_user, 1);
    //     S_List_File_print(Flight_sys.REGULAR_USER, REGULAR_FILE);
    // }
    // else if (new_user.grade == 5)
    // {
    //     printf("aaaaaaaaaaa");
    //     S_List_delete_1(Flight_sys.PLATINUM_USER, *log_data); // 传入指针的地址以修改原始指针
    //     S_List_insert(Flight_sys.PLATINUM_USER, new_user, 1);
    //     S_List_File_print(Flight_sys.PLATINUM_USER, PLATINUM_FILE);
    // }
    switch (new_user.grade)
    {
    case 2:

        // S_List_delete_1(Flight_sys.GOLD_USER, *log_data); // 传入指针的地址以修改原始指针
        S_List_delete_2(Flight_sys.GOLD_USER, log_data, new_passwd);
        // S_List_insert(Flight_sys.GOLD_USER, new_user, 1);
        S_List_File_print(Flight_sys.GOLD_USER, GOLD_FILE);
        break;
    case 3:

        // S_List_delete_1(Flight_sys.SILVER_USER, *log_data); // 传入指针的地址以修改原始指针
        S_List_delete_2(Flight_sys.SILVER_USER, log_data, new_passwd);
        // S_List_insert(Flight_sys.SILVER_USER, new_user, 1);
        S_List_File_print(Flight_sys.SILVER_USER, SILVER_FILE);
        break;
    case 4:

        // S_List_delete_1(Flight_sys.REGULAR_USER, *log_data); // 传入指针的地址以修改原始指针
        S_List_delete_2(Flight_sys.REGULAR_USER, log_data, new_passwd);
        // S_List_insert(Flight_sys.REGULAR_USER, new_user, 1);
        S_List_File_print(Flight_sys.REGULAR_USER, REGULAR_FILE);
        break;
    case 5:

        // S_List_delete_1(Flight_sys.PLATINUM_USER, *log_data); // 传入指针的地址以修改原始指针
        S_List_delete_2(Flight_sys.PLATINUM_USER, log_data, new_passwd);
        // S_List_insert(Flight_sys.PLATINUM_USER, new_user, 1);
        S_List_File_print(Flight_sys.PLATINUM_USER, PLATINUM_FILE);
        break;

    default:
        break;
    }

    return;
}


// 删除指定用户数据的函数
void S_List_delete_1(U_list *user_list, U_data target)
{
    U_list *current = user_list;
    U_list *previous = NULL;
    int found = 0;

    while (current != NULL)
    {
        if (strcmp(current->next->data.passwd, target.passwd) == 0 && strcmp(current->next->data.name, target.name) == 0)
        {
            found = 1;
            previous->next = current->next;
            free(current);
            break;
        }
        previous = current;
        current = current->next;
    }

    if (!found)
    {
        printf("Element not found in the list.\n");
    }
}

//覆盖写
void S_List_delete_2(U_list *user_list, U_data *log_data, char *new_passwd)
{
    if (strcmp(user_list->next->data.passwd, log_data->passwd) == 0 && strcmp(user_list->next->data.name, log_data->name) == 0)
    {
        strncpy(user_list->next->data.passwd, new_passwd, sizeof(user_list->next->data.passwd) - 1);
        user_list->next->data.passwd[sizeof(user_list->next->data.passwd) - 1] = '\0';
    }
    return;
}

